package com.jy.rhin.infra.repo.audit.query;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.framework.base.entity.Page;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.domain.audit.AuditInfoDao;
import com.jy.rhin.domain.audit.entity.AuditInfo;
import com.jy.rhin.facade.infra.controller.audit.assembler.AuditInfoAssembler;
import com.jy.rhin.infra.repo.audit.mapper.AuditInfoMapper;
import com.jy.rhin.infra.repo.audit.po.AuditEventInfoPO;
import com.jy.rhin.sdk.dto.audit.AuditDto;
import com.jy.rhin.sdk.dto.audit.AuditInfoEventDto;
import com.jy.rhin.sdk.dto.audit.AuditInfoReqDto;
import com.jy.rhin.sdk.dto.audit.AuditInfoRespDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dhl
 */
@Service
public class AuditInfoQueryHandler {
    @Resource
    private AuditInfoMapper auditInfoMapper;
    @Resource
    private AuditInfoDao auditInfoDao;

    public AuditDto getById(Long id) {
        AuditInfo auditInfo = auditInfoDao.getById(id);
        if (ObjectUtils.isEmpty(auditInfo)) {
            return null;
        }
        String jsonText = auditInfo.getJsonText();
        AuditDto auditDto = JSON.parseObject(jsonText, AuditDto.class);
        auditDto.setXml(auditInfo.getXmlText());
        //
        AuditInfoEventDto eventInfo = auditDto.getEventInfo();
        eventInfo.setShiJianJg(auditInfo.getShiJianJg());
        auditDto.setEventInfo(eventInfo);
        return auditDto;
    }

    public Page<AuditInfoRespDto> selectByPage(AuditInfoReqDto dto) {
        String kaiShi = dto.getKaiShiRQ();
        String jieShu = dto.getJieShuRQ();
        QueryWrapper<AuditEventInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(!StringUtils.isEmpty(dto.getShiJianLx()), AuditEventInfoPO::getShiJianLx, dto.getShiJianLx())
                .eq(!StringUtils.isEmpty(dto.getShiJianJg()), AuditEventInfoPO::getShiJianJg, dto.getShiJianJg())
                .func(!StringUtils.isEmpty(dto.getKaiShiRQ()), wrapper -> wrapper.ge(AuditEventInfoPO::getFaShengSj, Converter.toDate(kaiShi, Converter.DATE_FORMAT)))
                .func(!StringUtils.isEmpty(dto.getJieShuRQ()), wrapper -> wrapper.le(AuditEventInfoPO::getFaShengSj, Converter.toDate(jieShu + " 23:59:59", Converter.DATETIME_FORMAT)))
                .orderByDesc(AuditEventInfoPO::getFaShengSj)
        ;
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<AuditEventInfoPO> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(dto.getPageNo(), dto.getPageSize(), true);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<AuditEventInfoPO> selectResult = auditInfoMapper.selectPage(page, queryWrapper);

        Page<AuditInfoRespDto> resultPage = new Page<>();
        BeanUtils.copyProperties(selectResult, resultPage);
        List<AuditInfoRespDto> list = selectResult.getRecords()
                .stream()
                .map(AuditEventInfoPO::toDO)
                .map(AuditInfoAssembler::toDTO)
                .collect(Collectors.toList());
        resultPage.setRecords(list);
        return resultPage;
    }

}
